import { App, Configuration } from '@midwayjs/decorator';
import { ILifeCycle, IMidwayContainer } from '@midwayjs/core';
import { Application } from 'egg';
import * as orm from '@midwayjs/orm';
import * as axiso from '@midwayjs/axios';
import * as redis from '@midwayjs/redis';
import * as cache from '@midwayjs/cache';
import * as task from '@midwayjs/task';
import * as swagger from '@midwayjs/swagger';
import { join } from 'path';

@Configuration({
  imports: [orm, axiso, redis, cache, task, swagger],
  importConfigs: [join(__dirname, './config')],
  conflictCheck: true,
})
export class ContainerLifeCycle implements ILifeCycle {
  @App()
  app: Application;

  async onReady(contain: IMidwayContainer) {
    const httpService = await contain.getAsync(axiso.HttpService);
    httpService.interceptors.request.use(
      config => {
        return config;
      },
      error => {
        return Promise.reject(error);
      }
    );
  }
}
